package com.teamwhole.modules.grid.web;

import java.util.List;
import java.util.Map;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.alibaba.druid.support.json.JSONUtils;
import com.teamwhole.common.utils.StringUtils;
import com.teamwhole.modules.grid.entity.Facility;
import com.teamwhole.modules.grid.entity.Grid;
import com.teamwhole.modules.grid.service.FacilityService;
import com.teamwhole.modules.grid.service.GridService;

import jersey.repackaged.com.google.common.collect.Lists;
import jersey.repackaged.com.google.common.collect.Maps;

@Component
@Path("/grid/map")
@Consumes("application/json;charset=utf-8")
@Produces("application/json;charset=utf-8")
public class MapResource {

	@Autowired
	private GridService gridService;

	@Autowired
	private FacilityService facilityService;
	
	@SuppressWarnings("unchecked")
	@Path("list")
	@GET
	public Object list(@QueryParam("id") String id) {
		List<Map<String, Object>> list = Lists.newArrayList();
		
		// 区域
		Grid grid = new Grid();
		grid.setId(id);
		List<Grid> grids = gridService.findSingleGrid(grid);
		for (Grid item : grids) {
			Map<String, Object> obj = Maps.newHashMap();
			obj.put("type", "POLYGON");
			obj.put("id", item.getId());
			obj.put("dataType", "0");
			obj.put("strokeWeight", item.getLineWidth());
			obj.put("strokeColor", item.getLineColor());
			obj.put("fillColor", item.getBgColor());
			obj.put("text", item.getName());
			//obj.put("text", "");
			obj.put("center", item.getCenter().split(","));
			if(StringUtils.isNotBlank(item.getRegion())){
				Map<String, Object> coordinate = (Map<String, Object>) JSONUtils.parse(item.getRegion());
				obj.put("points", coordinate.get("points"));
			}else{
				obj.put("points", "");
			}
			
			Map<String, Object> messageData = Maps.newHashMap();
			messageData.put("code", item.getCode());
			messageData.put("name", item.getName());
			messageData.put("remark", item.getRemark());
			messageData.put("status", item.getStatus());
			obj.put("messageData", messageData);
			
			list.add(obj);
		}

		// 设施
		Facility facility = new Facility();
		facility.setGridID(id);
		List<Facility> facilities = facilityService.findList(facility);
		for (Facility item : facilities) {
			Map<String, Object> obj = Maps.newHashMap();
			obj.put("type", "POINT");
			obj.put("id", item.getId());
			obj.put("dataType", item.getType());
			//obj.put("text", item.getName());
			obj.put("text", "");
			obj.put("point", item.getSite().split(","));
			
			Map<String, Object> messageData = Maps.newHashMap();
			messageData.put("gridID", item.getGridID());
			messageData.put("gridName", item.getGridName());
			messageData.put("regionID", item.getRegionID());
			messageData.put("regionName", item.getRegionName());
			messageData.put("code", item.getCode());
			messageData.put("name", item.getName());
			messageData.put("function", item.getFunction());
			messageData.put("brand", item.getBrand());
			messageData.put("model", item.getModel());
			messageData.put("quantity", item.getQuantity());
			messageData.put("installTime", item.getInstallTime());
			messageData.put("recordID", item.getRecordID());
			messageData.put("image", item.getImage());
			messageData.put("status", item.getStatus());
			obj.put("messageData", messageData);
			list.add(obj);
		}
		
		Map<String, Object> map = Maps.newHashMap();
		map.put("overlays", list);
		return map;
	}

}
